package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;
    /**
     * 分页条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<PageResponse<RoleVo>> page(Integer pageNum, Integer pageSize, RoleDto dto) {
        //1.执行分页插件设置
        PageHelper.startPage(pageNum,pageSize);
        //2.根据条件查询角色表数据
        Page<RoleVo> page = roleMapper.page(dto.getRoleName());
        //3.封装并返回数据
        PageResponse<RoleVo> pageResponse=new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return ResponseResult.success(pageResponse);
    }

    /**
     * 新增角色
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult addRole(RoleDto dto) {
        Role role= BeanUtil.copyProperties(dto,Role.class);
        roleMapper.insertSelective(role);
        return ResponseResult.success();
    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     * @return
     */
    @Override
    public ResponseResult<Set<String>> findCheckedResources(Long roleId) {
        Set<String> set = roleResourceMapper.getRoleResourceNoByRoleId(roleId);
        return ResponseResult.success(set);
    }

    /**
     * 修改角色
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean updateRole(RoleDto dto) {
        //1.调用修改角色的mapper,直接执行修改role
        Role role=BeanUtil.copyProperties(dto,Role.class);
        int i = roleMapper.updateByPrimaryKeySelective(role);
        if(i==0){
            return  false;
        }
        //2.中间表数据操作
            //2.1 查询当前角色中是否包含资源数据，如果不包含，则结束
        String[] checkedResourceNos = dto.getCheckedResourceNos();
        if (StringUtils.isEmpty(checkedResourceNos)) {
            return false;
        }
        //2.2 包含，
        //  2.2.1先删除中间表数据，
        roleResourceMapper.deleteByRoleId(role.getId());
        //  2.2.2 然后在批量新增中间表数据
        List<RoleResource> roleResourcelist=new ArrayList<>();

        //把选中的资源编号的数组转成集合对象
        List<String> resourceNosList = Arrays.asList(checkedResourceNos);
        resourceNosList.forEach(resourceNo->{
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(resourceNo)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourcelist.add(roleResource);
        });

        if (EmptyUtil.isNullOrEmpty(roleResourcelist)) {
            return false;
        }

        int i1 = roleResourceMapper.batchInsert(roleResourcelist);
        //3.返回数据
        return i1==1;
    }


}
